package com.heu.blood.autologous.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heu.blood.autologous.dao.BloodInformationAutologousDao;
import com.heu.blood.autologous.dictenum.BloodStateAutologousEnum;
import com.heu.blood.autologous.dictenum.InventoryStateAutologousEnum;
import com.heu.blood.autologous.entity.BloodApplicationAutologousEntity;
import com.heu.blood.autologous.entity.BloodApplicationItemAutologousEntity;
import com.heu.blood.autologous.entity.BloodInformationAutologousEntity;
import com.heu.blood.autologous.exception.InventoryExceptionEnum;
import com.heu.blood.autologous.service.BloodInformationAutologousService;
import com.heu.blood.autologous.vo.BloodInventoryAutologousSelectVo;
import com.heu.blood.bag.entity.BloodBagNewRecycleEntity;
import com.heu.blood.globalExceptionHandler.MyException;
import com.heu.blood.match.exception.TimeException;
import com.heu.blood.storge.exception.InstorageException;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.autologous.dao.BloodInventoryAutologousDao;
import com.heu.blood.autologous.entity.BloodInventoryAutologousEntity;
import com.heu.blood.autologous.service.BloodInventoryAutologousService;
import org.springframework.transaction.annotation.Transactional;


@Service("bloodInventoryAutologousService")
public class BloodInventoryAutologousServiceImpl extends ServiceImpl<BloodInventoryAutologousDao, BloodInventoryAutologousEntity> implements BloodInventoryAutologousService {

    @Autowired
    BloodInventoryAutologousDao bloodInventoryAutologousDao;

    @Autowired
    BloodInventoryAutologousService bloodInventoryAutologousService;

    @Autowired
    BloodInformationAutologousDao bloodInformationAutologousDao;

    @Autowired
    BloodInformationAutologousService bloodInformationAutologousService;

    @Override
    public PageUtils queryPage(BloodInventoryAutologousSelectVo bloodInventoryAutologousSelectVo) {
        // 校验时间
        if (bloodInventoryAutologousSelectVo.getStartTime() != null
                && bloodInventoryAutologousSelectVo.getEndTime() != null
                && bloodInventoryAutologousSelectVo.getStartTime().after(bloodInventoryAutologousSelectVo.getEndTime())) {
            throw new MyException(TimeException.TIME_OUTLINE);
        }
        Map<String,Object> params = new HashMap<>();
        params.put("limit", bloodInventoryAutologousSelectVo.getLimit());
        params.put("page", bloodInventoryAutologousSelectVo.getPage());
        LambdaQueryWrapper<BloodInventoryAutologousEntity> queryWrapper = Wrappers.lambdaQuery(BloodInventoryAutologousEntity.class);

        queryWrapper.ge(bloodInventoryAutologousSelectVo.getStartTime() != null, BloodInventoryAutologousEntity::getAcknowledgmentTime, bloodInventoryAutologousSelectVo.getStartTime())
                .le(bloodInventoryAutologousSelectVo.getEndTime() != null, BloodInventoryAutologousEntity::getAcknowledgmentTime, bloodInventoryAutologousSelectVo.getEndTime())
                .like(Strings.isNotBlank(bloodInventoryAutologousSelectVo.getBloodId()),BloodInventoryAutologousEntity::getBloodId, bloodInventoryAutologousSelectVo.getBloodId())
                .like(Strings.isNotBlank(bloodInventoryAutologousSelectVo.getTransferStatus()),BloodInventoryAutologousEntity::getTransferStatus, bloodInventoryAutologousSelectVo.getTransferStatus())
                .like(Strings.isNotBlank(bloodInventoryAutologousSelectVo.getBloodType()),BloodInventoryAutologousEntity::getBloodType, bloodInventoryAutologousSelectVo.getBloodType())
                .like(Strings.isNotBlank(bloodInventoryAutologousSelectVo.getApplicationId()),BloodInventoryAutologousEntity::getApplicationId, bloodInventoryAutologousSelectVo.getApplicationId());

        IPage<BloodInventoryAutologousEntity> page = this.page(
                new Query<BloodInventoryAutologousEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<BloodInventoryAutologousEntity> getUnreceivedBloodByApplicationId(String applicationId) {

        LambdaQueryWrapper<BloodInventoryAutologousEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(applicationId),BloodInventoryAutologousEntity::getApplicationId,applicationId);
        List<BloodInventoryAutologousEntity> bloodInventoryAutologousEntities = bloodInventoryAutologousDao.selectList(queryWrapper);

        List<BloodInventoryAutologousEntity> list=new ArrayList<>();
        for (BloodInventoryAutologousEntity b : bloodInventoryAutologousEntities){
            String state = b.getTransferStatus();
            if (InventoryStateAutologousEnum.AUTO_PENDING_RECEIPT.code().equals(state)){
                list.add(b);
            }
        }

        return list;
    }

    @Transactional
    @Override
    public int confirmBloodReceipt(List<BloodInformationAutologousEntity> bloodInformationAutologousEntities) {
        List<String> bloodIdList = bloodInformationAutologousEntities.stream()
                .map(BloodInformationAutologousEntity::getBloodId)
                .collect(Collectors.toList());

        LambdaQueryWrapper<BloodInventoryAutologousEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(BloodInventoryAutologousEntity::getBloodId, bloodIdList);
        List<BloodInventoryAutologousEntity> inventoryList = bloodInventoryAutologousDao.selectList(queryWrapper);
        //更新库存表对应血袋状态为已签收
        for (BloodInventoryAutologousEntity bloodInventoryAutologousEntity : inventoryList){
            if (InventoryStateAutologousEnum.AUTO_RECEIVED.code().equals(bloodInventoryAutologousEntity.getTransferStatus())){
                throw new MyException(InventoryExceptionEnum.ALREADY_RECEIVED);
            }
            if (InventoryStateAutologousEnum.AUTO_DISPATCHED.code().equals(bloodInventoryAutologousEntity.getTransferStatus())){
                throw new MyException(InventoryExceptionEnum.ALREADY_SEND);
            }
            bloodInventoryAutologousEntity.setTransferStatus(InventoryStateAutologousEnum.AUTO_RECEIVED.code());
        }

        bloodInventoryAutologousService.updateBatchById(inventoryList);

        for (BloodInformationAutologousEntity bloodInformationAutologousEntity : bloodInformationAutologousEntities){
            bloodInformationAutologousEntity.setBloodState(BloodStateAutologousEnum.AUTO_IN_STOCK.code());
        }
        //插入血液信息表
        boolean success =bloodInformationAutologousService.saveOrUpdateBatch(bloodInformationAutologousEntities);

        return success ? bloodInformationAutologousEntities.size() : 0;

    }

    @Override
    public List<BloodInventoryAutologousEntity> echoInventoryInfo(List<String> bloodIds) {
        LambdaQueryWrapper<BloodInventoryAutologousEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(BloodInventoryAutologousEntity::getBloodId,bloodIds);
        List<BloodInventoryAutologousEntity> bloodInventoryAutologousEntities = bloodInventoryAutologousDao.selectList(queryWrapper);

        return bloodInventoryAutologousEntities;
    }

}